﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fwql.Net.Utils
{
    /// <summary>
    /// try get the value from a specified string.
    /// </summary>
    public static class TryGetValue
    {
        public static TryGetInt32Value Int32 { get; set; }
        public static TryGetInt64Value Int64 { get; set; }

        public class TryGetInt32Value
        {

            /// <summary>
            /// try convert a string to int.if there has some errors occur,then return zero.
            /// </summary>
            /// <param name="s"></param>
            /// <returns></returns>
            public static Int32 GetValueIfErrorThanZero(String s)
            {
                return GetValueIfErrorThanSpecified(s, 0);
            }

            /// <summary>
            /// try convert a string to int.if any error occur,then return the specified value.
            /// </summary>
            /// <param name="s"></param>
            /// <param name="specified"></param>
            /// <returns></returns>
            public static Int32 GetValueIfErrorThanSpecified(String s, Int32 specified)
            {
                Int32 res;

                if (!System.Int32.TryParse(s, out res))
                    res = specified;

                return res;
            }

            /// <summary>
            /// try convert a string to int.if any error occur,then throw a new exception.
            /// </summary>
            /// <param name="s"></param>
            /// <param name="errorMsg"></param>
            /// <returns></returns>
            public static Int32 GetValueIfErrorThanThrow(String s, string errorMsg)
            {
                Int32 res;

                if (!System.Int32.TryParse(s, out res))
                    throw new Exception(errorMsg);

                return res;
            }
        }

        public class TryGetInt64Value
        {

            /// <summary>
            /// try convert a string to long.if there has some errors occur,then return zero.
            /// </summary>
            /// <param name="s"></param>
            /// <returns></returns>
            public static Int64 GetValueIfErrorThanZero(String s)
            {
                return GetValueIfErrorThanSpecified(s, 0);
            }

            /// <summary>
            /// try convert a string to long.if any error occur,then return the specified value.
            /// </summary>
            /// <param name="s"></param>
            /// <param name="specified"></param>
            /// <returns></returns>
            public static Int64 GetValueIfErrorThanSpecified(String s, Int64 specified)
            {
                Int64 res;

                if (!System.Int64.TryParse(s, out res))
                    res = specified;

                return res;
            }

            /// <summary>
            /// try convert a string to long.if any error occur,then throw a new exception.
            /// </summary>
            /// <param name="s"></param>
            /// <param name="errorMsg"></param>
            /// <returns></returns>
            public static Int64 GetValueIfErrorThanThrow(String s, string errorMsg)
            {
                Int64 res;

                if (!System.Int64.TryParse(s, out res))
                    throw new Exception(errorMsg);

                return res;
            }
        }

    }
}
